package de.wojenko.sps;

import java.io.File;
import java.util.List;

import javax.xml.bind.JAXBException;

import com.google.common.collect.Lists;

public class FileSprintDataImportService implements SprintDataImportService<XmlUserStory> {

	public FileSprintDataImportService() {
	}

	@Override
	public SprintData<XmlUserStory> importSprint(File file) throws JAXBException {
		XmlSprint result = new XmlSprint();
		result.setName(cutSuffix(file.getName()));
		Rss rss = JAXBUtils.unmarshall(file, Rss.class, Channel.class, Item.class);

		List<XmlUserStory> userStoryList = Lists.newArrayList();
		result.setBacklogItemList(userStoryList);
		for (Item item : rss.channel.itemList) {
			String[] parts = StringUtils.normalize(item.summary).split(" ",2);
			for (String string : parts) {
				System.out.println("'"+string+"'");
			}
			String number = parts[0];
			String title = StringUtils.normalize(parts[1]);
			XmlUserStory userStory = new XmlUserStory();
			userStory.setName(title);
			userStory.setId(number);
			userStory.setContent(StringUtils.normalize(item.description));
			userStoryList.add(userStory);
		}

		return result;
	}
	
	private String cutSuffix(String fileName) {
		return fileName.substring(0, fileName.lastIndexOf("."));
	}
}
